
export default {
  data() {
    return {
      provinceList: [],
      cityList: [],
      areaList: [],
      locationInfo: {
        provinceId: null,
        provinceName: '',
        cityId: null,
        cityName: '',
        areaId: null,
        areaName: ''
      },
      autoFillCityId: false,
      autoFillAreaId: false
    }
  },
  created() {

  },
  methods: {
    async getLocationList(parentId = 0, type = 'provinceList') {
      try {
        const getLocationListResult = await this.$api.getLocationList({ parentId, page: 1, pageSize: 999 })
        this[type] = getLocationListResult.list
      } catch (err) {
        console.error(err)
      }
    },
    async handleLocatinChangeEvent(event, type = 'area') {
      console.log('location change: ', type)
      if (type === 'province') {
        const index = this.provinceList.findIndex(res => res.geoAreaId === event)
        if (index >= 0) {
          this.locationInfo.provinceName = this.provinceList[index].areaName
        }
        await this.getLocationList(event, 'cityList')
        if (this.cityList.length > 0 && this.autoFillCityId) {
          this.locationInfo.cityId = this.cityList[0].geoAreaId
          this.locationInfo.cityName = this.cityList[0].areaName
          await this.getLocationList(this.locationInfo.cityId, 'areaList')
          if (this.areaList.length > 0 && this.autoFillAreaId) {
            this.locationInfo.areaId = this.areaList[0].geoAreaId
            this.locationInfo.areaName = this.areaList[0].areaName
          }
        } else {
          this.locationInfo.cityId = ''
          this.locationInfo.cityName = ''
          this.locationInfo.areaId = ''
          this.locationInfo.areaName = ''
        }
      } else if (type === 'city') {
        const index = this.cityList.findIndex(res => res.geoAreaId === event)
        if (index >= 0) {
          this.locationInfo.cityName = this.cityList[index].areaName
        }
        await this.getLocationList(event, 'areaList')
        if (this.areaList.length > 0 && this.autoFillAreaId) {
          this.locationInfo.areaId = this.areaList[0].geoAreaId
        } else {
          this.locationInfo.areaId = ''
          this.locationInfo.areaName = ''
        }
      } else {
        const index = this.areaList.findIndex(res => res.geoAreaId === event)
        if (index >= 0) {
          this.locationInfo.areaName = this.areaList[index].areaName
        }
      }
    }
  }
}
